<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('联麓推广短信列表')"/>
    <style>
        .input-css{
            border: 1px solid #ddd;
            border-radius: 4px;
            background: transparent;
            outline: none;
            height: 30px;
            width: 60px;
            padding-left: 5px;
        }
    </style>
</head>

<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <input type="hidden" name="memberIdStr" id="memberIdStr" th:value="${memberIdStr}">
        <div class="col-sm-12 search-collapse">
            <p class="select-title">通知短信余量：【<span th:text="${notifyNum}"></span>】条</p>
            <p class="select-title">营销短信余量：【<span th:text="${salesNum}"></span>】条</p>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
</body>
<script th:inline="javascript">
    const productTypeDict = [[${@dict.getType('product_type')}]];
    const prefix = ctx + "back/lianLuSms";
    $(function () {
        var options = {
            url: prefix + "/searchTemplateList",
            modalName: "联麓推广短信短信模板",
            paginationShowPageGo: true,
            clickToSelect: true,
            columns: [
                {
                    field: 'templateName',
                    title: '模板名称',
                },
                {
                    field: 'id',
                    title: '模板ID',
                    visible: false
                },
                {
                    field: 'content',
                    title: '短信内容',
                    formatter: function (value, row, index) {
                        var actions = [];
                        var regexp = new RegExp("{%s[1-9]%}","gi");
                        var replaceText = '<input class="input-css" type="text" name="' + row.id + '[]"/>';
                        actions.push('<span>' + value.replace(regexp, replaceText) + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'ctime',
                    title: '创建时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-primary btn-xs" href="#" onclick="send(\'' + row.id + '\',\''+row.content.replaceAll("\n",'')+'\')"><i class="fa fa-send">发送</i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.memberIdStr = $('#memberIdStr').val();
        return search;
    }

    /* 发送短信-提交 */
    function send(templateCode,content) {
        console.log(templateCode);
        console.log(content);
        var data = {"memberIdStr": $('#memberIdStr').val(), "templateCode": templateCode};
        var param = [];
        var regexp = new RegExp("{%s[1-9]%}");
        $('input[name="'+templateCode+'[]"]').each(function(){
            param.push($(this).val());
            content = content.replace(regexp,$(this).val());
        });
        data.templateParamArr = param;
        $.modal.confirm("短信内容【"+content+"】，请确认是否发送？", function() {
            $.operate.save(prefix + "/send", data);
        })
    }

</script>
</html>